622C - Not Equal on a Segment - CodeForces Solution


data structures implementation *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    for (int i = 0; i < n; ++i ) cin >> a[i];
    vector<int> prec(n, -1);
    for (int i = 1; i < n; ++i){
        if (a[i - 1] != a[i]) prec[i] = i - 1;
        else prec[i] = prec[i - 1];
    }
    for (int i = 0; i < m; ++i){
        int l, r, x;
        cin >> l >> r >> x;
        if (a[r - 1] != x) cout << r;
        else if (prec[r - 1] >= l - 1) cout << prec[r - 1] + 1;
        else cout << -1;
        cout << "\n";
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push
706B - Interesting drink
1265A - Beautiful String
214A - System of Equations
287A - IQ Test
1108A - Two distinct points
1064A - Make a triangle
1245C - Constanze's Machine
1005A - Tanya and Stairways
1663F - In Every Generation
1108B - Divisors of Two Integers
1175A - From Hero to Zero
1141A - Game 23
1401B - Ternary Sequence
598A - Tricky Sum
519A - A and B and Chess
725B - Food on the Plane
154B - Colliders
127B - Canvas Frames